using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using OVS.BusinessObjects;

namespace OVS.DAL
{
    public partial class CameraDAO
	{
        public Camera GetCamera_By_Nom(String came_nom)
        {
            try
            {
                Camera oCamera = new Camera();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [came_id],[came_loc],[came_url],[came_desc],[came_nom] FROM [Camera] WHERE [came_nom]=@came_nom", CommandType.Text);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom", DbType.String, came_nom));
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    oCamera.came_id = Convert.ToInt32(oDbDataReader["came_id"]);
                    oCamera.came_loc = Convert.ToString(oDbDataReader["came_loc"]);
                    oCamera.came_url = Convert.ToString(oDbDataReader["came_url"]);

                    if (oDbDataReader["came_desc"] != DBNull.Value)
                        oCamera.came_desc = Convert.ToString(oDbDataReader["came_desc"]);

                    if (oDbDataReader["came_nom"] != DBNull.Value)
                        oCamera.came_nom = Convert.ToString(oDbDataReader["came_nom"]);
                }
                oDbDataReader.Close();
                return oCamera;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        public int RemoveCamera_By_Nom(String came_nom)
        {

            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [Camera] WHERE [came_nom]=@came_nom", CommandType.Text);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom", DbType.String, came_nom));
                return DbProviderHelper.ExecuteNonQuery(oDbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public int UpdateCamera_By_Nom(Camera oCamera)
        {

            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [Camera] SET [came_loc]=@came_loc,[came_url]=@came_url,[came_desc]=@came_desc,[came_nom]=@came_nom WHERE [came_nom]=@came_nom", CommandType.Text);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_loc", DbType.String, oCamera.came_loc));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_url", DbType.String, oCamera.came_url));
                if (oCamera.came_desc != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_desc", DbType.String, oCamera.came_desc));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_desc", DbType.String, DBNull.Value));
                if (oCamera.came_nom != null)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom", DbType.String, oCamera.came_nom));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_nom", DbType.String, DBNull.Value));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_id", DbType.Int32, oCamera.came_id));
                return DbProviderHelper.ExecuteNonQuery(oDbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<Camera> GetCamerasEnabled()
        {       
			try
			{
				List<Camera> lstCameras = new List<Camera>();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [came_id],[came_loc],[came_url],[came_desc],[came_nom],[came_use] FROM [Camera] WHERE [came_use]='True'", CommandType.Text);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Camera oCamera = new Camera();
					oCamera.came_id = Convert.ToInt32(oDbDataReader["came_id"]);
					oCamera.came_loc = Convert.ToString(oDbDataReader["came_loc"]);
					oCamera.came_url = Convert.ToString(oDbDataReader["came_url"]);

					if(oDbDataReader["came_desc"] != DBNull.Value)
						oCamera.came_desc = Convert.ToString(oDbDataReader["came_desc"]);

					if(oDbDataReader["came_nom"] != DBNull.Value)
						oCamera.came_nom = Convert.ToString(oDbDataReader["came_nom"]);

					if(oDbDataReader["came_use"] != DBNull.Value)
						oCamera.came_use = Convert.ToBoolean(oDbDataReader["came_use"]);
					lstCameras.Add(oCamera);
				}
				oDbDataReader.Close();
				return lstCameras;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
    }
}
